//
//  VisitorLoginView.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/6.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

//---------------------------------------------------------
//声明协议 需要继承基协议
//面试题： OC中没有多继承 可以用协议来代替
//optional 是OC中的关键字 如果在swift中用OC中的一些机制 需要在前面加@objc来告诉编译器该声明兼容OC
//---------------------------------------------------------

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {

    //声明协议方法   可选的协议方法
    optional func userWillRegister()
    //登录的协议的方法
    func userWillLogin()
}

class VisitorLoginView: UIView {
    
//------声明代理对象 在OC中必须使用weak assign 为了防止循环引用--------
    weak var visitorDelegate: VisitorLoginViewDelegate?
   
    //外部控制器调用
    func setupInfoWith(title: String,imageName: String?) {
        
        tipLabel.text = title
        
        if imageName != nil {
            //不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏房子
            largeHouse.hidden = true
            
            bringSubviewToFront(circleIcon)
        }else {
            //首页圈圈转动
            startAnimation()
        }
    }
    
    func startAnimation() {
     //基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        animation.toValue = 2 * M_PI
        
//默认移除动画 在动画结束时 或者页面处于非活跃状态时 动画效果不移除图层
        animation.removedOnCompletion = false
        //添加到图层
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
 
    //要定义自定义视图 先找到入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        //访客视图的自定义
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    //MARK:自定义访客视图
    private func setupUI() {
        // 添加子视图 
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //VFL 使用父视图添加约束对象
        //......................
        
        //框架设置约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //设置整个访客视图的颜色
        let value: CGFloat = 236.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //添加按钮的点击事件 Selector类型的方法选择器 无法将字符串对引用实例化为Selector类型的方法选择器 
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
  
    }
    //按钮的监听事件
    @objc private func loginBtnClick() {
    //打印当前调用函数
        print(__FUNCTION__)
        visitorDelegate?.userWillLogin()
    }
    @objc private func registerBtnClick() {
    
        print(__FUNCTION__)
        visitorDelegate?.userWillRegister?()
    }
  
//懒加载子视图
    //蒙板视图
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圆圈
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //标签
    private lazy var tipLabel: UILabel = UIFactory.labelFactory("", textColor: UIColor.darkGrayColor(), fontSize: 16)
    
    //登陆按钮
    private lazy var loginBtn: UIButton = UIFactory.buttonWithTitle("登录", backImageName: "common_button_white_disable", textColor: UIColor.darkGrayColor(), fontSize: 16)
    
    //注册按钮
    private lazy var registerBtn: UIButton = UIFactory.buttonWithTitle("注册", backImageName: "common_button_white_disable", textColor: themeColor, fontSize: 16)
 
}
